import Vue from 'vue'
import Vuex from 'vuex'
import Promise from 'core-js/fn/promise'
import { setToken,removeToken } from "@/utils/token"
import { setUid,removeUser} from "@/utils/user"
import {reqUserRegister,reqPwdUpdata,reqUserLogin,reqGoodsList,reqStateUpdata,reqGetUser,reqCategoryList} from "@/api"
Vue.use(Vuex)

//actions用于响应组件中的动作
const actions={
    //用户注册
    async userRegister(context,user){
        //console.log(user)
       const result = await reqUserRegister(user);
       if (result.code == "200") {
        alert(result.msg)
        //注册成功
        return 'ok';
        } else {
        //注册失败
        alert(result.msg)
        return 'fail';
        }
    },

    //用户登录
    async userLogin(context,data){
        const result = await reqUserLogin(data);
        if(result.data.tokenValue){
            context.commit("USERLOGIN",result);
            setToken(result.data.tokenValue)
            setUid(result.data.loginId)
            return result.msg;
        } else {
        return Promise.reject(new Error('fail'));
            }
    },

    //修改密码
    async pwdUpdate(context,data){
        const result = await reqPwdUpdata(data);
        if(result.code == 200){
            return 'ok';
        } else {
        return 'fail';
            }
    },

    //获取商品信息
    async goodsListInfo(context){
        const result = await reqGoodsList();
        if(result.code == 200){
            context.commit("GETGOODSLIST",result.data)
        }else{
            return Promise.reject(new Error('fail'));
        }
    },

    //修改商品状态
    async gstateUpdate(context,data){
        const result = await reqStateUpdata(data);
        //console.log(result)
        if(result.code == 200){
            return 'ok';
        }else{
            return Promise.reject(new Error('fail'));
        }
    },
    //获取用户信息
    async getUserInfo(context,data){
        const result = await reqGetUser(data);
        if(result.code == 200){
            return result.data;
        }else{
            return Promise.reject(new Error('fail'));
        }
    },
    //卖家登出
    userLogout(context){
        context.commit("CLEAR");
    },

    //搜索商品
    searchGoods(context,data){
        context.commit("GETGOODSLIST",data)
    },
    //获取商品分类
    async categoryListInfo(context){
        const result = await reqCategoryList();
        if(result.code == 200){
            context.commit("CATEGORYLIST",result.data);
        }else{
            return Promise.reject(new Error('fail'));
        }
    }
}

//mutations用于操作数据（state)
const mutations={
    //登录功能
    USERLOGIN(state,result){
        state.token=result.uname;
        state.uid=result.uid
    },
    //登出功能
   CLEAR(state){
    state.token='';
    state.uid='';
    removeToken();
    removeUser()
   },
   //获取商品分类
   CATEGORYLIST(state,data){
    state.categoryList=data
   },
   //获取商品列表
   GETGOODSLIST(state,data){
    //console.log(data)
    state.goodsList=data
   }
}
//state 用于储存数据
const state={
    goodsList:[],
    token:localStorage.getItem('TOKEN'),
    uid:localStorage.getItem('UID'),
    categoryList:[]
}

const getters={
   gList(state){
    return state.goodsList
   },
   cList(state){
    return state.categoryList
   }
}

export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})